<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Producto {

    var $total_tables = array();

    public function __construct() {
        $this->CI = & get_instance();
        $this->CI->config->load('database_tables');
        $this->total_tables = $this->CI->config->item('dbtables');
    }

    public function products() {
        $user = $this->CI->ion_auth->user()->row();
        $this->CI->db->where('fk_id_'.$this->total_tables['empresa'], $user->company);
        return $this->CI->db->get($this->total_tables['producto']);
    }
    
    public function count_all() {
        $user = $this->CI->ion_auth->user()->row();
        $this->CI->db->where('fk_id_'.$this->total_tables['empresa'], $user->company);
        $query = $this->CI->db->get($this->total_tables['producto']);
        return $query->num_rows();
    }

    public function product($id = NULL) {
        //if no id was passed use the current product id
        $id || $id = $this->CI->session->userdata('product_id');

        $this->CI->db->limit(1);
        $this->CI->db->where($this->total_tables['producto'] . '.id', $id);
        return $this->CI->db->get($this->total_tables['producto']);
    }
}